$(function () {

    //图标跟随鼠标移动
    let aims = document.querySelector('#aims')
    document.onmousemove = ((aa) => {
        let x = aa.clientX
        let y = aa.clientY
        aims.style.top = y - aims.height / 2 + 20 + 'px';
        aims.style.left = x - aims.width / 2 + 20 + 'px';
        aims.style.display = "block";
    })

    //加载动画
    animation()

    function animation() {
        let show = document.querySelector('.show')
        let hide = document.querySelector('.hide')

        function auto() {
            show.style.display = 'none'
            hide.style.display = 'block'
        }
        let set = setTimeout(auto, 2000)
    }

    //表单验证
    checkcode()

    function checkcode() {

         fetch('../../api/user.json')
        .then(result => result.json())
        .then(data => {
            // console.log(data)
            codenNumber()
            add(data)
        })

        // $.ajax({
        //     type:'get',
        //     url:'Access-Control-Allow-Origin:https://gitee.com/acrya/bankone/blob/master../../api/user.json',
        //     dataType:'json',
        //     success:function(data){
        //         console.log(data)
        //     },
        //     error:function(){
        //         console.log('request error')
        //     }
        // })

        let compellation = document.querySelector('.name')
        let id_card = document.querySelector('.id_card')
        let id_number = document.querySelector('.id_number')
        let phone = document.querySelector('.phone')
        let code = document.querySelector('.code')
        let create_code = document.querySelector('.create_code')
        let check = document.querySelector('.check')
        let agree = document.querySelector('.agree')
        let next = document.querySelector('.next')
        let reset = document.querySelector('.reset')
        agree.disabled = 'disabled'
        next.disabled = 'disabled'

        //下一步
        function add(data) {
            next.onclick = () => {
                let arr = {
                    "name": compellation.value,
                    "type": id_number.value,
                    "IDNumber": id_number.value,
                    "phoneNum": phone.value
                }
                data.push(arr)
                console.log(data)

                if (agree.checked != 1) {
                    alert('亲，请勾选个人协议！')
                } else {
                    window.location.href = '../../html/frontEnd/success.html'
                }
            }
        }

        //重置
        reset.onclick = () => {
            compellation.value = ''
            id_card.value = ''
            id_number.value = ''
            phone.value = ''
            code.value = ''
            agree.disabled = 'disabled'
            codenNumber()
        }

        // 验证码

        function codenNumber() {
            let codes
            create_code.addEventListener('click', () => {
                create()
            })
            check.addEventListener('click', () => {
                checks()
            })
            let set = setTimeout(create, 100)

            function create() {
                codes = ''
                let codeLength = 4
                let random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                    'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 's', 'd', 'f', 'g', 'h', 'j',
                    'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p')
                for (let i = 0; i < codeLength; i++) {
                    let index = Math.floor(Math.random() * 60)
                    codes += random[index]
                }
                create_code.value = codes
            }

            function checks() {
                if (code.length <= 0) {
                    alert('亲，请输入验证码！')
                } else if (code.value != codes) {
                    alert('亲，您输入的验证码不正确！')
                    create()
                    code.value = ''
                } else {
                    alert('验证成功！^-^')
                    agree.disabled = ''
                    next.disabled = ''
                }
            }
        }
       
    }

    
})